//
//  LocationSearchRequest.swift
//  MyWeatherApp
//
//  Created by Shengbing Li (QXU7859) on 2021/2/27.
//  Copyright © 2021 shanghai. All rights reserved.
//

import Alamofire
import SwiftyJSON
import ObjectMapper


/*
 /api/location/search/?query=san
 /api/location/search/?query=london
 /api/location/search/?lattlong=36.96,-122.02
 /api/location/search/?lattlong=50.068,-5.316

 */
enum QueryType {
    case query(String)
    case lattlongTuple((latitude: Double, longitude: Double))
}

class LocationSearchRequest: APIRequest {
    typealias APIResourceType = LocationSearchResource
    
    var path: String
    var method: HTTPMethod
    var parameters: Parameters?
    
    init(query: QueryType) {
        self.method = .get
        switch query {
        case let .query(value):
            self.path = "/api/location/search/?query=\(value)"
        case let .lattlongTuple(tuple):
            self.path = "/api/location/search/?lattlong=\(tuple.latitude),\(tuple.longitude)"
        }
        
        self.parameters = ["": ""]
    }
}

class LocationSearchResource: APIResource {
    
    var title: String? {
        return raw?[0]["title"].string
    }
    var location_type: String? {
        return raw?[0]["location_type"].string
    }
    var woeid: Int? {
        return (raw?[0]["woeid"].int)
    }
    var latt_long: String? {
        return (raw?[0]["latt_long"].string)
    }
    
    var debugDescription: String {
        return """
        LocationSearchResource log:
            title: \(title ?? ""),
            location_type: \(location_type ?? ""),
            woeid: \(woeid ?? 0),
            latt_long: \(latt_long ?? "")
        """
    }
}


class LocationSearchModel: Mappable {
    var title: String?
    var location_type: String?
    var woeid: Int?
    var latt_long: String?
    
    required init(map: Map) {
        
    }
    
    // Mappable
    func mapping(map: Map) {
        title <- map["title"]
        location_type <- map["location_type"]
        woeid <- map["woeid"]
        latt_long <- map["latt_long"]
    }
    
    var debugDescription: String {
        return """
        LocationSearchModel log:
            title: \(title ?? ""),
            location_type: \(location_type ?? ""),
            woeid: \(woeid ?? 0),
            latt_long: \(latt_long ?? "")
        """
    }
}


